package org.jeecg.modules.wflow.workflow.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.wflow.bean.entity.WflowModelHistorys;
import org.jeecg.modules.wflow.bean.entity.WflowNotifys;
import org.jeecg.modules.wflow.mapper.WflowModelHistorysMapper;
import org.jeecg.modules.wflow.mapper.WflowNotifysMapper;
import org.jeecg.modules.wflow.utils.UserUtil;
import org.jeecg.modules.wflow.workflow.bean.dto.NotifyDto;
import org.jeecg.modules.wflow.workflow.service.NotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.GregorianCalendar;
import java.util.List;
import java.util.Optional;

/**
 * 消息通知服务
 *
 * @author : willian fu
 * @date : 2022/9/17
 */
@Slf4j
@Service
public class DefaultNotifyServiceImpl implements NotifyService {

    @Autowired
    private WflowNotifysMapper notifysMapper;

    @Autowired
    private WflowModelHistorysMapper modelHistorysMapper;

    @Override
    public void notify(NotifyDto notify) {
        WflowModelHistorys model = modelHistorysMapper.selectOne(new QueryWrapper<WflowModelHistorys>()
                .select("settings").eq("process_def_id", notify.getProcessDefId()));
        Optional.ofNullable(model).ifPresent(md -> {
            JSONObject.parseObject(md.getSettings()).get("notify");
            //保存通知消息
            notifysMapper.insert(WflowNotifys.builder()
                    .id(IdUtil.objectId())
                    .title(notify.getTitle())
                    .instanceId(notify.getInstanceId())
                    .content(notify.getContent())
                    .userId(notify.getTarget())
                    .link(null)
                    .type(notify.getType())
                    .readed(false)
                    .createTime(null == notify.getCreateTime() ?
                            GregorianCalendar.getInstance().getTime()
                            : notify.getCreateTime())
                    .build());
            //TODO 可以调用websocket实时推送通知
            log.info("推送通知[{}]给[{} 内容为{}]", notify.getTarget(), notify.getTitle(), notify.getContent());
        });
    }

    @Override
    public Page<WflowNotifys> getNotify(Integer pageSize, Integer pageNo) {
        return notifysMapper.selectPage(new Page<>(pageNo, pageSize),
                new QueryWrapper<WflowNotifys>()
                        .eq("readed", false)
                        .eq("user_id", UserUtil.getLoginUserId())
                        .orderByDesc("create_time")
        );
    }

    @Override
    public void readNotify(List<String> ids) {
        notifysMapper.update(WflowNotifys.builder().readed(true).build(),
                new QueryWrapper<WflowNotifys>().in("id", ids)
                        .eq("user_id", UserUtil.getLoginUserId()));
    }
}
